package com.djq.hhc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.djq.hhc.dataprocess.ImgLoadUtils;
import com.djq.hhc.dto.HomeFamilyDto;
import com.djq.hhc.dto.HomeFlowerDto;
import com.djq.hhc.model.home.HomeFamily;
import com.djq.hhc.model.home.HomeFlower;
import com.djq.hhc.model.post.PostInfo;
import com.djq.hhc.result.Result;
import com.djq.hhc.service.HomeFlowerService;
import com.djq.hhc.vo.HomeFlowerVo;
import com.djq.hhc.vo.PostInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/03/05/下午 5:00
 * @Description:
 */

@RestController
@RequestMapping(value = "/home/homeFlower")
public class HomeFlowerController {

    @Autowired
    private HomeFlowerService homeFlowerService;

    //返回所有family, 不包含时间
    @GetMapping("/getAllFlower")
    public Result getAllFlower() {
        List<HomeFlower> list = homeFlowerService.list();
        List<HomeFlowerDto> res = list.stream()
                .map(homeFlower -> new HomeFlowerDto(homeFlower.getId(), homeFlower.getFlowerName()))
                .collect(Collectors.toList());
        return Result.ok(res);
    }


    //修改
    @PutMapping("update")
    public Result updateById(@RequestBody HomeFlower homeFlower) {
        return homeFlowerService.updateFlowerById(homeFlower);
    }

    //删除
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Integer id) {
        Boolean isOk = homeFlowerService.removeById(id);
        if (isOk) {
            return Result.ok(null).message("删除花朵成功");
        } else {
            return Result.fail(null).message("删除花朵失败");
        }
    }

    //通过id获取
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        HomeFlower homeFlower = homeFlowerService.getById(id);
        return Result.ok(homeFlower);
    }

    //添加
    @PostMapping("save")
    public Result save(@RequestBody HomeFlower homeFlower) {
        return homeFlowerService.saveFlower(homeFlower);
    }

    //分页
    @PostMapping("{page}/{limit}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody(required = false) HomeFlowerVo homeFlowerVo) {
        //创建page对象，传递当前页和每页记录数
        Page<HomeFlower> pageParam = new Page<>(page, limit);
        //获取条件值
        String flowerName = homeFlowerVo.getFlowerName();
        String details = homeFlowerVo.getDetails();
        Integer family = homeFlowerVo.getFamily();
        //封装条件
        QueryWrapper<HomeFlower> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(flowerName)) {
            wrapper.like("flower_name", flowerName);
        }
        if (!StringUtils.isEmpty(details)) {
            wrapper.like("details", details);
        }
        if (!StringUtils.isEmpty(family)) {
            wrapper.eq("family", family);
        }
        //调用方法得到分页查询结果
        IPage<HomeFlower> pageModel = homeFlowerService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    /**
     * 下载图片
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        String s = ImgLoadUtils.upLoad(file);
        return Result.ok(s);
    }

    /**
     * 下载图片
     * @param name
     * @param response
     */
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response){
        ImgLoadUtils.downLoad(name, response);
    }
}
